package com.qiku.service.impl;

import com.qiku.dao.CourseMapper;
import com.qiku.domain.Course;
import com.qiku.domain.CourseVo;
import com.qiku.domain.Teacher;
import com.qiku.service.CourseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 多条件查询
     * @param courseVo  封装的前端请求参数
     * @return
     */
    @Override
    public List<Course> findCourseByCondition(CourseVo courseVo) {
        return courseMapper.findCourseByCondition(courseVo);
    }

    /**
     * 保存课程信息和讲师信息
     * @param courseVo
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public void saveCourseOrTeacher(CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {
        //封装课程信息
        Course course = new Course();
        BeanUtils.copyProperties(course,courseVo);
        //补全信息
        //创建当前时间对象
        Date date = new Date();
        course.setCreateTime(date);
        course.setUpdateTime(date);
        //保存课程信息
        courseMapper.saveCourse(course);
        //获取课程的id
        int courseId = course.getId();
        //封装讲师信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher,courseVo);
        //补全信息
        teacher.setCourseId(courseId);
        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        //保存讲师信息
        courseMapper.saveTeacher(teacher);
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Override
    public CourseVo findCourseById(Integer id) {
        return courseMapper.findCourseById(id);
    }

    /**
     * 修改课程信息和讲师信息
     * @param courseVo
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public void updateCourseOrTeacher(CourseVo courseVo) throws InvocationTargetException, IllegalAccessException {
        //封装课程信息
        Course course = new Course();
        BeanUtils.copyProperties(course,courseVo);
        //补全信息
        //创建当前时间对象
        Date date = new Date();
        course.setUpdateTime(date);
        //修改课程信息
        courseMapper.updateCourse(course);
        //封装讲师信息
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher,courseVo);
        //补全信息
        teacher.setCourseId(course.getId());
        teacher.setUpdateTime(date);
        //修改讲师信息
        courseMapper.updateTeacher(teacher);
    }

    /**
     * 修改课程状态
     * @param id
     * @param status
     */
    @Override
    public void updateCourseStatus(int id, int status) {
        Course course = new Course();
        //创建当前时间对象
        Date date = new Date();
        course.setUpdateTime(date);
        course.setId(id);
        course.setStatus(status);
        courseMapper.updateCourseStatus(course);
    }


}
